package com.marvels.financial.utils;

import java.math.BigDecimal;
import java.util.List;

import com.marvels.financial.loan.dto.McfeCustomerLoanContractDTO;
import com.marvels.financial.loan.dto.McfeCustomerRepaymentPlanDTO;

/**财务工具类
 * @author hejie
 */
public class FinancialUtil { 
	/**检查还款计划是否是还清状态
	 * @param planDTO  还款计划
	 * @return true表示已经还清，false表示未还清
	 */
	public static Boolean checkPlanStatus(McfeCustomerRepaymentPlanDTO planDTO) {
		//本金
		BigDecimal principalOS = planDTO.getPrincipal().subtract(planDTO.getPrincipalAct()).subtract(planDTO.getPrincipalWaive());
		//利息
		BigDecimal interestOS = planDTO.getInterest().subtract(planDTO.getInterestAct()).subtract(planDTO.getInterestWaive());
		//咨询费
		BigDecimal consultingFeeOS = planDTO.getConsultingFee().subtract(planDTO.getConsultingFeeAct()).subtract(planDTO.getConsultingFeeWaive());
		//管理费
		BigDecimal managementFeeOS = planDTO.getManagementFee().subtract(planDTO.getManagementFeeAct()).subtract(planDTO.getManagementFeeWaive());
		//罚息
		BigDecimal penaltyFeeOS = planDTO.getPenaltyFee().subtract(planDTO.getPenaltyFeeAct()).subtract(planDTO.getPenaltyFeeWaive());
		//滞纳金
		BigDecimal lateFeeOS = planDTO.getLateFee().subtract(planDTO.getLateFeeAct()).subtract(planDTO.getLateFeeWaive());
		
		BigDecimal zero = BigDecimal.ZERO;
		principalOS = principalOS.compareTo(zero)<0?zero:principalOS;
		interestOS = interestOS.compareTo(zero)<0?zero:interestOS;
		consultingFeeOS = consultingFeeOS.compareTo(zero)<0?zero:consultingFeeOS;
		managementFeeOS = managementFeeOS.compareTo(zero)<0?zero:managementFeeOS;
		penaltyFeeOS = penaltyFeeOS.compareTo(zero)<0?zero:penaltyFeeOS;
		lateFeeOS = lateFeeOS.compareTo(zero)<0?zero:lateFeeOS;
		
		BigDecimal amount = principalOS.add(interestOS).add(consultingFeeOS).add(managementFeeOS).add(penaltyFeeOS).add(lateFeeOS);
		if(amount.compareTo(zero)>0) {
			return false;
		}else {
			return true;
		}
	}
	 
	/**检查还款计划是否是还清状态
	 * @param planDTO  还款计划
	 * @return true表示已经还清，false表示未还清
	 */
	public static Boolean checkContract(McfeCustomerLoanContractDTO contractDTO) {
		//本金
		BigDecimal principalOS =contractDTO.getPrincipalOs();
		//利息
		BigDecimal interestOS = contractDTO.getInterestOs();
		//咨询费
		BigDecimal consultingFeeOS = contractDTO.getConsultingFeeOs();
		//管理费
		BigDecimal managementFeeOS = contractDTO.getManagementFeeOs();
		//罚息
		BigDecimal penaltyFeeOS = contractDTO.getPenaltyFeeOs();
		//滞纳金
		BigDecimal lateFeeOS = contractDTO.getLateFeeOs();
		
		BigDecimal zero = BigDecimal.ZERO;
		principalOS = principalOS.compareTo(zero)<0?zero:principalOS;
		interestOS = interestOS.compareTo(zero)<0?zero:interestOS;
		consultingFeeOS = consultingFeeOS.compareTo(zero)<0?zero:consultingFeeOS;
		managementFeeOS = managementFeeOS.compareTo(zero)<0?zero:managementFeeOS;
		penaltyFeeOS = penaltyFeeOS.compareTo(zero)<0?zero:penaltyFeeOS;
		lateFeeOS = lateFeeOS.compareTo(zero)<0?zero:lateFeeOS;
		
		BigDecimal amount = principalOS.add(interestOS).add(consultingFeeOS).add(managementFeeOS).add(penaltyFeeOS).add(lateFeeOS);
		if(amount.compareTo(zero)>0) {
			return false;
		}else {
			return true;
		}
	}
	
	
	/**通过还款计划 检查 合同状态
	 * @param planList
	 * @param contractDTO
	 * @return
	 */
	public static Boolean checkContractStatus(List<McfeCustomerRepaymentPlanDTO> planList,McfeCustomerLoanContractDTO contractDTO) {
		if(planList==null || planList.size() ==0) {
			return false;
		}
		//遍历还款计划 客户贷款状态 0正常 1逾期 2结清 3提前结清 10核销
		boolean settle = true;
		for(McfeCustomerRepaymentPlanDTO plan:planList) {
			Integer status = plan.getStatus();
			if(status==1) {//出现逾期记录
				settle = false;
				contractDTO.setDelqDays(plan.getDelqDays());
				contractDTO.setCurrTenor(plan.getTenor()-1);
				break;
			}else if(status == 0){//第一条正常状态的
				settle = false;
				contractDTO.setDelqDays(0);
				contractDTO.setForceFlag(0);
				contractDTO.setCurrTenor(plan.getTenor()-1);
				contractDTO.setStatus(0);
				break;
			}
		}
		if(settle) {//结清
			contractDTO.setDelqDays(0);
			contractDTO.setForceFlag(0);
			contractDTO.setCurrTenor(contractDTO.getTenor());
			contractDTO.setStatus(2);
			//调用结清A00
		}
		return true;
	}
//	3、检查合同做处理
//	SELECT c.delq_days,c.force_flag,c.curr_tenor,c.`status`,c.* FROM mp_contract_account_2_info c WHERE c.id = 合同id ;
//	   3.1、还款计划中无逾期期数 合同表改 delq_days = 0 ， force_flag = 'A0' , curr_tenor = 还款计划中所有A2最大的tenor值，如果无A2的默认0，status = A0
//	   3.2、还款计划中有逾期的期数 合同表改 delq_days = 逾期部分还款计划delq_days最大的那个， curr_tenor = 还款计划中所有A2最大的tenor值，如果无A2的默认0，status = A1
//	   3.3、结清合同表 delq_days = 0 ， force_flag = 'A0'  ， curr_tenor = 还款计划中所有A2最大的tenor值，如果无A2的默认0，status = A2
//	        结清申请表 调用 景山接口修改  ac_loan_apply_info 的status为 A00
}
